package com.ks.user.service.impl;

import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ks.core.base.OperationContext;
import com.ks.core.constant.Const;
import com.ks.core.dto.SearchCriteriaBase;
import com.ks.core.exception.BizExceptionEnum;
import com.ks.core.exception.GunsException;
import com.ks.core.util.ToolUtil;
import com.ks.user.dao.UserBaseMapper;
import com.ks.user.entity.OrgBase;
import com.ks.user.entity.OrgEmployee;
import com.ks.user.dao.OrgEmployeeMapper;
import com.ks.user.entity.UserBase;
import com.ks.user.service.IOrgEmployeeService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ks.user.service._ServiceBase;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 组织员工数据 服务实现类
 * </p>
 *
 * @author kingdee123
 * @since 2020-11-30
 */
@Service
public class OrgEmployeeServiceImpl extends _ServiceBase<OrgEmployeeMapper, OrgEmployee> implements IOrgEmployeeService {
    @Resource
    private OrgEmployeeMapper orgEmployeeMapper;
    @Resource
    private UserBaseMapper userBaseMapper;


    @Override
    public List<OrgEmployee> getEmployeeList(int NodeId) {
        EntityWrapper<OrgEmployee> wrapper = new EntityWrapper<>();
        OrgEmployee orgEmployee = new OrgEmployee();
        orgEmployee.setCOrgNodeID(NodeId);
        orgEmployee.setSStatus(Const.DEL_FLAG_NORMAL);
        wrapper.setEntity(orgEmployee);
        wrapper.and("iUserID != null");
        wrapper.and("iUserID > 0");
        List<OrgEmployee> orgEmployees = orgEmployeeMapper.selectList(wrapper);
        return orgEmployees;
    }

    @Override
    public List<OrgEmployee> getList(OperationContext operationContext) {
        EntityWrapper<OrgEmployee> wrapper = new EntityWrapper<>();
        OrgEmployee orgEmployee = new OrgEmployee();
        orgEmployee.setDelMark(Const.DEL_FLAG_NORMAL);
        orgEmployee.setSStatus(Const.DEL_FLAG_NORMAL);
        orgEmployee.setTenantID(operationContext.getTenantId().intValue());
        wrapper.setEntity(orgEmployee);
        List<OrgEmployee> orgEmployees = orgEmployeeMapper.selectList(wrapper);
        return orgEmployees;
    }

    @Override
    public Map<String, Object> getEmployeeList(OperationContext operationContext, SearchCriteriaBase searchCriteriaBase) {
        String tenantId = operationContext.getTenantId().toString();
        EntityWrapper<OrgEmployee> wrapper = new EntityWrapper<>();
        OrgEmployee orgEmployee = new OrgEmployee();
        orgEmployee.setTenantID(Integer.valueOf(tenantId));
        orgEmployee.setDelMark(Const.DEL_FLAG_NORMAL);
        Map<String, Object> map = new HashMap<>();
        ArrayList<SearchCriteriaBase.matchField> fields = searchCriteriaBase.getFields();
        if (ToolUtil.isNotEmpty(fields)) {
            for (SearchCriteriaBase.matchField field : fields) {
                if ("iname".equals(field.getName())) {
                    orgEmployee.setIName(field.getValue());
                }
                if ("imobile".equals(field.getName())) {
                    orgEmployee.setIMobile(field.getValue());
                }
            }
        }
        ArrayList<SearchCriteriaBase.StateField> states = searchCriteriaBase.getStates();
        if (ToolUtil.isNotEmpty(states)) {
            for (SearchCriteriaBase.StateField state : states) {
                if ("sstatus".equals(state.getName())) {
                    orgEmployee.setSStatus(Integer.valueOf(state.getValue()));
                }
            }
        }
        wrapper.setEntity(orgEmployee);
        Integer integer = orgEmployeeMapper.selectCount(wrapper);
        map.put("count", integer);
        List<OrgEmployee> orgEmployees = orgEmployeeMapper.selectPage(new Page(searchCriteriaBase.getPageIndex(), searchCriteriaBase.getLimit()), wrapper);
        for (OrgEmployee employee : orgEmployees){
            if (ToolUtil.isNotEmpty(employee.getIUserID())){
                UserBase userBase = userBaseMapper.selectById(employee.getIUserID());
                employee.setIsAdmin(userBase.getIAdmin());
            }

        }
        map.put("list", orgEmployees);
        return map;

    }

    @Override
    public int addOrgEmployee(OperationContext operationContext, OrgEmployee orgEmployee) {
        UserBase userBase = this.LoadUserDataByToken(operationContext.getToken());
        UserBase userBase1 = new UserBase();
        userBase1.setITel(orgEmployee.getIMobile());
        userBase1.setTenantID(operationContext.getTenantId().intValue());
        UserBase base = userBaseMapper.selectOne(userBase1);
        if (ToolUtil.isNotEmpty(base)) {
            throw new GunsException(BizExceptionEnum.MOBILE_REPEAT);
        }
        if (ToolUtil.isNotEmpty(userBase)) {
            orgEmployee.setCreateUserID(userBase.getId());
            orgEmployee.setUpdateUserID(userBase.getId());
            orgEmployee.setTenantID(operationContext.getTenantId().intValue());
            orgEmployee.setOrgSubID(orgEmployee.getCOrgNodeID());
            orgEmployee.setOrgID(operationContext.getOrgId().intValue());
        }
        Integer insert = orgEmployeeMapper.insert(orgEmployee);
        return insert;
    }

    @Override
    public int updateOrgEmployee(OperationContext operationContext, OrgEmployee orgEmployee) {
        UserBase userBase = this.LoadUserDataByToken(operationContext.getToken());
        if (ToolUtil.isNotEmpty(userBase)) {
            Integer integer = orgEmployeeMapper.updateById(orgEmployee);
            return integer;
        }
        return 0;
    }

    @Override
    public OrgEmployee getOrgEmployee(int employeeId) {
        OrgEmployee orgEmployee = orgEmployeeMapper.selectById(employeeId);
        if (ToolUtil.isNotEmpty(orgEmployee)) {
            return orgEmployee;
        }
        return null;
    }

    @Override
    public int delOrgEmployee(OperationContext operationContext, int employeeId) {
        OrgEmployee orgEmployee = orgEmployeeMapper.selectById(employeeId);
        if (ToolUtil.isNotEmpty(orgEmployee)) {
            orgEmployee.setDelMark(Const.DEL_FLAG_DEL);
            Integer integer = orgEmployeeMapper.updateById(orgEmployee);
            return integer;
        }
        return 0;
    }

    @Override
    public OrgEmployee getEmployeeInfo(int userId) {
        OrgEmployee orgEmployee = new OrgEmployee();
        orgEmployee.setIUserID(userId);
        orgEmployee.setDelMark(Const.DEL_FLAG_NORMAL);
        orgEmployee.setSStatus(Const.DEL_FLAG_NORMAL);
        OrgEmployee employee = orgEmployeeMapper.selectOne(orgEmployee);
        return employee;
    }
}
